package com.gm.app.web;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.gm.app.data.IndicationDAO;
import com.gm.app.jasperreports.RegisterEntry;
import com.gm.app.model.Indication;
import com.gm.app.model.Register;
import com.gm.app.model.Resident;

@RequestMapping("/report/**")
@Controller
public class ReportController {

	@RequestMapping("indication/{reportName}")
	public ModelAndView indications(
			Model map,
			@PathVariable("reportName") String reportName,
			@RequestParam(value = "format", required = false, defaultValue = "pdf") String format) {

		Query q = IndicationDAO.get().getEntityManager()
				.createNamedQuery(reportName);
		List<RegisterEntry> entries = new ArrayList<RegisterEntry>();
		for (Object o : q.getResultList()) {
			Object[] reg = (Object[]) o;
			RegisterEntry r = new RegisterEntry();
			entries.add(r.fluidName((String) reg[0])
					.fluidEmail((String) reg[1])
					.fluidHomePhone((String) reg[2])
					.fluidCommercialPhone((String) reg[3])
					.fluidAddress((String) reg[4])
					.fluidCountry((String) reg[5])
					.fluidComments((String) reg[6]));
		}
		map.addAttribute("indications", new JRBeanCollectionDataSource(entries,
				false));
		return new ModelAndView(reportName, map.asMap());
	}

	@RequestMapping("resident/{reportName}")
	public ModelAndView residents(
			Model map,
			@PathVariable("reportName") String reportName,
			@RequestParam(value = "format", required = false, defaultValue = "pdf") String format) {

		Query q = IndicationDAO.get().getEntityManager()
				.createNamedQuery(reportName);
		List<RegisterEntry> entries = new ArrayList<RegisterEntry>();
		for (Object o : q.getResultList()) {
			Object[] reg = (Object[]) o;
			RegisterEntry r = new RegisterEntry();
			entries.add(r.fluidName((String) reg[0])
					.fluidEmail((String) reg[1])
					.fluidHomePhone((String) reg[2])
					.fluidCommercialPhone((String) reg[3])
					.fluidAddress((String) reg[4])
					.fluidCountry((String) reg[5])
					.fluidComments((String) reg[6]));
		}
		map.addAttribute("residents", new JRBeanCollectionDataSource(entries,
				false));
		return new ModelAndView(reportName, map.asMap());
	}

	@RequestMapping("indication/map")
	public String indicationsMap(Model map) {
		List<Indication> regs = Indication.findAllIndications();
		map.addAttribute("count", regs.size());
		map.addAttribute("list", regs);

		return "report/register/maps";
	}

	@RequestMapping("resident/map")
	public String residentsMap(Model map) {
		List<Resident> regs = Resident.findAllResidents();
		map.addAttribute("count", regs.size());
		map.addAttribute("list", regs);
		return "report/register/maps";
	}

	@RequestMapping("register/map")
	public String registerMap(Model map) {
		List<Register> regs = Register.findAllRegisters();
		map.addAttribute("count", regs.size());
		map.addAttribute("list", regs);
		return "report/register/maps";
	}

	@RequestMapping
	public String index() {
		return "report/index";
	}
}
